package main

import "fmt"

// ResultJson 封装统一返回类
type ResultJson struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

// Success 返回成功
func (r *ResultJson) Success(result any) *ResultJson {
	r.Code = 200
	r.Msg = "ok"
	r.Data = result
	return r
}

// Fail 返回失败
func (r *ResultJson) Fail(code int, msg string) *ResultJson {
	r.Code = code
	r.Msg = msg
	return r
}

// FailAnyData 返回失败
func (r *ResultJson) FailAnyData(code int, msg string, result any) *ResultJson {
	r.Code = code
	r.Msg = msg
	r.Data = result
	return r
}

func (r *ResultJson) String() string {
	dataStr := ""
	if r.Data != nil {
		dataStr = fmt.Sprintf("%v", r.Data)
	}
	return fmt.Sprintf("ResultJson{Code:%d, Msg:%s, Data:%s}", r.Code, r.Msg, dataStr)
}

func (r *ResultJson) stringFormat() string {
	dataStr := ""
	if r.Data != nil {
		dataStr = fmt.Sprintf("%v", r.Data)
	}
	return fmt.Sprintf("{code:%d, msg:%s, data:%s}", r.Code, r.Msg, dataStr)
}
